<?php

class JSONDBSingle
{
	var $filepath = null;

	function __construct($file){
		$this->filepath=$file;
	}	

	
	function all(){
		$lines = explode("\n",file_get_contents($this->filepath));
		$json=array();
		foreach($lines as $line){
			if($line != ""){
				$json[] = json_decode($line);
			}
		}
		return $json;
	}
	function edit($line,$data){
		$lines = explode("\n",file_get_contents($this->filepath));
		$lines[$line]=json_encode($data);
		if (!$handle = fopen($this->filepath, 'w')) {
	         echo "Cannot open file ($filename)";
	         exit;
	    	}
		fwrite($handle, implode("\n",$lines)); 
		fclose($handle);
	}
	
	function add($data){
		$data_json = json_encode($data);
		
		if (!$handle = fopen($this->filepath, 'a')) {
	         echo "Cannot open file ($filename)";
	         exit;
	    	}
		fwrite($handle, "$data_json\n"); 
		fclose($handle);	
	}
	
	function get($line){
		$json = $this->all();
		return $json[$line];
	}
	

}
?>